package com.example.textvertical;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;

/**
 * Created by ata on 17-8-8.
 * 竖直文字排版
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class SuperTextView extends View {

    private String text = "白日依山尽,黄河入海流,欲穷千里目,更上一层楼,fgdsgdsfg";
    private Rect textBounds;
    private int numLine = 1;
    private int textWidth;
    private SparseArray<String> textInfo;
    private int indexText;
    private TextPaint tPaint;
    private int mFontHeight;
    private Rect rect;
    private Paint mPaint;
    private int baseLine;
    private int textSpace = 10;
    private int verticalSpace = 10;


    public SuperTextView(Context context) {
        this(context, null);
    }

    public SuperTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public SuperTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);


        tPaint = new TextPaint();
        tPaint.setTextAlign(Paint.Align.LEFT);
        tPaint.setAntiAlias(true);


        textBounds = new Rect();
        textInfo = new SparseArray<>();
        tPaint.setTextSize(40);
    }

    public void setTextSpace(int textSpace) {
        this.textSpace = textSpace;
    }

    public void setVerticalSpace(int verticalSpace) {
        this.verticalSpace = verticalSpace;
    }

    private void init() {
        Paint.FontMetrics fm = tPaint.getFontMetrics();
        mFontHeight = (int) (Math.ceil(fm.descent - fm.top) * 1.0) + verticalSpace;

        for (int i = 0; i < text.length(); i++) {
            tPaint.getTextBounds(text, i, i + 1, textBounds);
            int columnsHeight = mFontHeight * ((i + 1) - indexText);
            if (text.charAt(i) == '\n') {
                indexText = i;
                numLine++;
            } else if (columnsHeight > getMeasuredHeight()) {
                indexText = i;
                numLine++;
            } else if (i * mFontHeight < getMeasuredHeight() && text.charAt(i) == '\n') {
                indexText = i;
                numLine++;
            } else if (columnsHeight == getMeasuredHeight()) { // 最后一行
                textInfo.append(numLine, text.substring(indexText, i + 1));
                textWidth = textWidth > textBounds.width() ? textWidth : textBounds.width();
                indexText = i;
                numLine += 1;
            }
            textInfo.append(numLine, text.substring(indexText, i + 1));
            textWidth = textWidth > textBounds.width() ? textWidth : textBounds.width() + textSpace;
        }
        baseLine = (int) ((mFontHeight - verticalSpace) / 2 + (fm.bottom - fm.top) / 2 - fm.bottom);
        Log.d("init_baseLine", "init: " + baseLine);

    }

    private void setText(String text) {
        this.text = text;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        init();
        rect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        for (int i = 1; i <= numLine; i++) {
            String s = textInfo.get(i);
            for (int f = 0; f < s.toCharArray().length; f++) {
                canvas.drawText(s, f, f + 1, getMeasuredWidth() - i * textWidth, mFontHeight * f + baseLine, tPaint);
                tPaint.getTextBounds(s, f, f + 1, rect);
                canvas.drawRect(
                        ((getMeasuredWidth()) - i * textWidth) + rect.left,
                        (baseLine + rect.top) + f * mFontHeight,
                        (getMeasuredWidth() - i * textWidth) + rect.right,
                        (baseLine + rect.bottom) + f * mFontHeight, mPaint);
            }
        }
    }
}
